package com.wnx.job.modules.jms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.PageResult;
import com.wnx.job.common.api.QueryPageBean;
import com.wnx.job.common.mapstruct.PostMapper;
import com.wnx.job.common.utils.StrUtils;
import com.wnx.job.modules.jms.dto.PostParam;
import com.wnx.job.modules.jms.mapper.JmsPostMapper;
import com.wnx.job.modules.jms.model.JmsPost;
import com.wnx.job.modules.jms.service.JmsPostService;
import com.wnx.job.modules.mini.dto.PostMiniParam;
import com.wnx.job.modules.pms.model.PmsTag;
import com.wnx.job.modules.pms.service.PmsTagService;
import com.wnx.job.nosql.elasticsearch.document.EsPost;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Service
public class JmsPostServiceImpl extends ServiceImpl<JmsPostMapper, JmsPost> implements JmsPostService {
    @Resource
    private JmsPostMapper postMapper;
    @Resource
    private PmsTagService tagService;


    /**
     * 根据职位ID查询职位信息
     * @param postId
     * @return
     */
    @Override
    public PostParam findById(Long postId) {
        return postMapper.findById(postId);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<PostParam> findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<PostParam> postList = postMapper.findByCondition(queryPageBean.getQueryParams());
        postList.forEach(x->{
            List<PmsTag> list = tagService.list(new QueryWrapper<PmsTag>().lambda().eq(PmsTag::getPostId, x.getId()));
            x.setTagList(list);
        });
        return PageResult.restPage(postList);

    }

    /**
     * 新增职位信息
     * @param postParam
     */
    @Override
    public void savePost(PostParam postParam) {
        //调用持久层，进行职位新增。
        List<PmsTag> tagList = postParam.getTagList();
        JmsPost tbPost = PostMapper.INSTANCE.toPojo(postParam);
        tbPost.setCreated(new Date());
        postMapper.insert(tbPost);
        //做完职位新增后，再集体做标签新增
        savePostTag(tagList,tbPost.getId());
    }
    /**
     * 修改职位信息
     * @param post
     */
    @Override
    public void updatePost(PostParam post) {
        List<PmsTag> tagList = post.getTagList();

        if (tagList !=null && tagList.size() > 0){
            //清空该职位全部的标签记录
            long count = tagService.findTagCountByPostId(post.getId());
            if ( count > 0 ){tagService.deleteByPostId(post.getId());}

            // 依次新增新的标签记录
            savePostTag(tagList,post.getId());
        }

        //修改职位信息
        JmsPost tbPost = PostMapper.INSTANCE.toPojo(post);
        tbPost.setUpdated(new Date());
        postMapper.updateById(tbPost);
    }

    /**
     * 查看全部职位信息
     * @return
     */
    @Override
    public List<EsPost> findAll() {
        List<EsPost> postList = postMapper.findAll();
        postList.forEach(x->{
            List<PmsTag> list = tagService.list(new QueryWrapper<PmsTag>().lambda().eq(PmsTag::getPostId, x.getId()));
            x.setTagList(list);
        });
        return postList;

    }

    /**
     * 获取职位信息
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<EsPost> getPostList(QueryPageBean queryPageBean) {
        List<EsPost> postList = null;
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //处理前端选项卡查询
        Map<String, Object> queryParams = queryPageBean.getQueryParams();
        String keyword = (String) queryParams.get("keyword");
        if (StringUtils.isNotBlank(keyword)){
            switch (keyword){
                case "最具人气":
                    postList = baseMapper.findAllOrderByHit();
                    break;
                case "最新发布":
                    postList = baseMapper.findAllOrderByCreated();
                    break;
                case "最具价值":
                    postList = baseMapper.findAllOrderBySalary();
                    break;
                default:
                    postList = baseMapper.findAll();
            }
        }else {
            postList = baseMapper.findAll();
        }


        //对职位列表信息进行处理
        postList.forEach(x->{
            x.setDescription(StrUtils.CutString(x.getDescription(),100));
            x.setCompanyDescription(StrUtils.CutString(x.getCompanyDescription(),100));
        }
        );
        //设置职位标签信息
        postList.forEach(x->{
            List<PmsTag> list = tagService.list(new QueryWrapper<PmsTag>().lambda().eq(PmsTag::getPostId, x.getId()));
            x.setTagList(list);
        });

        return PageResult.restPage(postList);
    }

    /**
     * 根据ID职位信息（网站前台）
     * @param id
     * @return
     */
    @Override
    public EsPost getPostDetailById(Long id) {
        EsPost esPost = baseMapper.getPostDetailById(id);
        //设置职位标签信息
        List<PmsTag> tagList = tagService.list(new QueryWrapper<PmsTag>().lambda().eq(PmsTag::getPostId, esPost.getId()));
        esPost.setTagList(tagList);
        return esPost;
    }

    /**
     * 查询Mini的全部职位信息
     * @return
     */
    @Override
    public List<PostMiniParam> finMiniAll() {
        return baseMapper.findMiniAll();
    }

    /**
     * 获取热度文章
     * @return
     */
    @Override
    public List<PostMiniParam> findHostPostList() {
        List<PostMiniParam> postList = baseMapper.findMiniAll();
        return postList.stream().limit(10).collect(Collectors.toList());
    }


    /**
     * 新增职位标签
     * @param tagList
     * @param postId
     */
    private void savePostTag(List<PmsTag> tagList,Long postId){
        if (tagList != null && tagList.size() > 0){
            tagList.forEach(tag -> {
                tag.setCreated(new Date());
                tag.setStatus(Const.STATUS_ON);
                tag.setPostId(postId);
                tagService.save(tag);
            });

        }
    }

    /**
     * 根据职位ID查询职位标签集合
     * @param postId
     * @return
     */
    @Override
    public List<PmsTag> findTagListByPostId(Long postId) {
        return tagService.list(new QueryWrapper<PmsTag>().eq("post_id",postId));
    }



}
